<template>
  <!-- 查看详情（车队向司机派单） 2018/3/7 11:44 zhangQ -->
  <div>
    <Tabs v-model="subOrderOpera.currentTab">
      <template v-for="(item,index) in fleetTabs">
        <TabPane :label="item.label" :name="item.val"></TabPane>
      </template>
    </Tabs>

    <!-- 已完成 -->
    <keep-alive>
      <complete v-if="subOrderOpera.currentTab=='0' && subOrderOpera.afterShow"
                :completeData="subOrderOpera.completeData" @viewDetailImg="viewDetailImgAction"
                :currentMainOrder="currentMainOrder"></complete>
    </keep-alive>

    <!-- 已经派发司机 -->
    <keep-alive>
      <dispatchComplete v-if="subOrderOpera.currentTab=='1'" :dispatchCompleteData="subOrderOpera.dispatchCompleteData"
                        @refreshSource="refreshSource" @viewDetailImg="viewDetailImgAction"
                        :driverList="driverList" :currentMainOrder="currentMainOrder"></dispatchComplete>
    </keep-alive>

    <!-- 未派发司机 -->
    <keep-alive>
      <dispatchNot v-if="subOrderOpera.currentTab=='2'" :dispatchNotData="subOrderOpera.dispatchNotData"
                   :driverList="driverList" :addressList="addressList" @refreshSource="refreshSource"
                   @viewDetailImg="viewDetailImgAction" :currentMainOrder="currentMainOrder"></dispatchNot>
    </keep-alive>

    <!-- 未选订单 -->
    <keep-alive>
      <subOrderNotCheck v-if="subOrderOpera.currentTab=='3'" :subOrderNotCheckData="subOrderOpera.subOrderNotCheckData"
                        @viewDetailImg="viewDetailImgAction" @refreshSource="refreshSource"
                        @bindSuccess="subOrderOpera.currentTab='2'"
                        :currentMainOrder="currentMainOrder"></subOrderNotCheck>
    </keep-alive>

    <xlinkModal v-model="ImgModel" title="图片" className="vertical-center-modal" width="700px;">
      <viewMedia :lookImtList="lookImtList" v-if="ImgModel"></viewMedia>
    </xlinkModal>
  </div>
</template>

<script>
  import { findByMainOrderNoForDispatchStateFG } from '@api/backEnd/mainOrder'
  // import { getAddressList } from '@api/backEnd/station'
  import { getdriverlist } from '@api/backEnd/fleetControl'
  import { mapGetters } from 'vuex'
  import dispatchComplete from './components/dispatchComplete'
  import dispatchNot from './components/dispatchNot'
  import subOrderNotCheck from './components/subOrderNotCheck'
  import complete from './components/complete'
  import xlinkModal from '@components/modal/xlinkModal'
  import viewMedia from '../viewMedia'

  export default {
    name: 'index',
    data () {
      let fleetTabs = [
        { label: '已完成', val: '0' },
        { label: '已派单', val: '1' },
        { label: '未派单', val: '2' },
        { label: '未挑选', val: '3' }
      ]
      let subOrderOpera = {
        afterShow: false,
        currentTab: '0',
        completeData: [],
        dispatchCompleteData: [],
        dispatchNotData: [],
        subOrderNotCheckData: []
      }
      let driverList = []
      let addressList = []
      let ImgModel = false
      let lookImtList = []
      let datas = { fleetTabs, subOrderOpera, driverList, addressList, ImgModel, lookImtList }
      return datas
    },
    props: {
      currentMainOrder: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    computed: {
      ...mapGetters(['currentUser'])
    },
    methods: {
      /**
       * TODO(获得子运单信息)
       * @auther zhangQ
       * @date 2018/3/16 15:40
       **/
      getSubOrderData () {
        if (!(this.currentMainOrder && this.currentMainOrder.orderNo)) {
          return
        }
        let param = {
          orderNo: this.currentMainOrder.orderNo
        }
        findByMainOrderNoForDispatchStateFG(param).then(res => {
          if (res.data.isSuccess === 1) {
            let subOrderData = res.data.data
            if (subOrderData && subOrderData.length > 0) {
              this.subOrderBranch(subOrderData)
            }
            console.log('subOrderData', subOrderData)
            console.log('subOrderOpera', this.subOrderOpera)
          } else {
            Promise.reject(new Error(res.data.res))
          }
        }).catch((err) => {
          console.log('接口findByMainOrderNoForDispatchState:' + err)
        })
      },
      /**
       * TODO(子订单分支)
       * @auther zhangQ
       * @date 2018/3/26 18:03
       */
      subOrderBranch (subOrderData) {
        subOrderData.forEach((item) => {
          if (!item.actFleetId || (item.actFleetId && item.actFleetId === this.currentUser.userId)) {
            if (item.voucherImg && item.fleetId && item.fleetId === this.currentUser.userId && item.driverId) {
              this.subOrderOpera.completeData.push(item)
            } else if (item.fleetId && item.fleetId === this.currentUser.userId && (item.actFleetId === this.currentUser.userId || item.groupFleetId === this.currentUser.userId) && item.fleetDispatch === 1) {
              this.subOrderOpera.dispatchCompleteData.push(item)
            } else if (item.fleetId && item.fleetId === this.currentUser.userId && (item.fleetDispatch === 0 || (item.fleetDispatch === 1 && item.groupFleetId !== this.currentUser.userId))) {
              this.subOrderOpera.dispatchNotData.push(item)
            } else if (!item.fleetId) {
              this.subOrderOpera.subOrderNotCheckData.push(item)
            } else {
              console.log('other data', item)
            }
          }
        })
      },
      driverlist () {
        getdriverlist({
          page: 1,
          pageSize: 500
        }).then(res => {
          if (+res.data.isSuccess === 1) {
            console.log('this.addressList', this.addressList)
            let resData = res.data
            this.driverList = resData.data.list
          } else {
            console.log(res.data.res)
          }
        })
        // getAddressList().then(res => {
        //   console.log('this.addressList', this.addressList)
        //   if (res.data.isSuccess === 1) {
        //     this.addressList = res.data.data
        //   }
        // })
      },
      viewDetailImgAction (row) {
        this.lookImtList = JSON.parse(row.voucherImg)
        this.ImgModel = true
      },
      refreshSource () {
        this.subOrderOpera = Object.assign(this.subOrderOpera, {
          completeData: [],
          dispatchCompleteData: [],
          dispatchNotData: [],
          subOrderNotCheckData: []
        })
        this.getSubOrderData()
      }
    },
    mounted () {
      this.getSubOrderData()
      this.driverlist()
      setTimeout(() => {
        this.subOrderOpera.afterShow = true
      }, 200)
    },
    components: { xlinkModal, viewMedia, dispatchComplete, dispatchNot, subOrderNotCheck, complete }
  }
</script>

<style scoped>

</style>
